package com.zhenjizhenpin.mybatis.binding;

import com.zhenjizhenpin.mybatis.mapping.MappedStatement;
import com.zhenjizhenpin.mybatis.mapping.SqlCommandType;
import com.zhenjizhenpin.mybatis.session.Configuration;
import com.zhenjizhenpin.mybatis.session.SqlSession;
import lombok.Getter;

import java.lang.reflect.Method;

public class MapperMethod {

    private final SqlCommand command;

    public MapperMethod(Class<?> mapperInterface, Method method, Configuration configuration){
        this.command = new SqlCommand(configuration, mapperInterface, method);
    }

    public Object execute(SqlSession sqlSession, Object[] args){
        Object result = null;
        switch (command.getType()){
            case INSERT:
            case DELETE:
            case UPDATE:
                break;
            case SELECT:
                result = sqlSession.selectOne(command.getName(), args);
                break;
            default:
                throw new RuntimeException("unknown execution method for: " + command.getName());
        }
        return result;
    }

    @Getter
    public static class SqlCommand{
        private final String name;
        private final SqlCommandType type;


        public SqlCommand(Configuration config, Class<?> mapperInterface, Method method){
            String statementName = mapperInterface.getName() + "." + method.getName();
            MappedStatement ms = config.getMappedStatement(statementName);
            name = ms.getId();
            type = ms.getSqlCommandType();
        }
    }


}
